package com.administrator.customviewtest.annoation;

import android.app.Activity;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/8/18 09 06
 * lsp
 */

public class InjectUtil {
    public static void inject(Activity activity) {
        Class myclass = activity.getClass();
        try {
            //通过反射获取到findviewByid这个方法
            Method method = myclass.getMethod("findViewById", int.class);
            //获取所有的属性
            Field[] Fields = myclass.getDeclaredFields();
            //遍历属性，看是否有注解
            for (Field field : Fields) {
                InjectView anoa = field.getAnnotation(InjectView.class);
                if (anoa!=null){
                    //执行方法
                    Object view = method.invoke(activity,anoa.value());
                    field.setAccessible(true);
                    field.set(activity,view);

                }
            }
        } catch (Exception e) {
            Log.e("ssss",e.toString());
            e.printStackTrace();
        }

    }
}
